package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Salary;
import cn.wolfcode.crm.domain.TaskExecute;
import cn.wolfcode.crm.mapper.SalaryMapper;
import cn.wolfcode.crm.query.impl.SalaryQuery;
import cn.wolfcode.crm.service.ISalaryService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Service
public class SalaryServiceImpl implements ISalaryService {
    @Autowired
    private SalaryMapper mapper;

    public PageResult query(SalaryQuery qo) {
        int rows = mapper.queryForCount(qo);
        if (rows==0){
            return new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<Salary> data = mapper.queryForList(qo);
        return new PageResult(rows,data);
    }

    public void saveOrUpdate(Salary salary) {
        if (salary.getId()==null){
            mapper.insert(salary);

        }else{
            mapper.updateByPrimaryKey(salary);
        }
    }

    public void exportXls(HttpServletResponse response)throws IOException{
        //文件下载响应头
        response.setHeader("Content-disposition", "attachment;filename=task.xls");
        //创建excel文件(workbook)
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = workbook.createSheet("day01");
        //标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("员工姓名");
        row.createCell(1).setCellValue("基本工资");
        row.createCell(2).setCellValue("奖金");
        row.createCell(3).setCellValue("扣薪");
        row.createCell(4).setCellValue("计薪方式");
        row.createCell(5).setCellValue("计税方式");
        row.createCell(6).setCellValue("是否缴纳基金");
        row.createCell(7).setCellValue("领薪日");
        row.createCell(8).setCellValue("总薪资");
        //查询员工数据
        List<Salary> list = mapper.selectAll();
        for (int i = 0; i < list.size(); i++) {
            Salary salary = list.get(i);
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(salary.getEmployee().getUsername());
            row.createCell(1).setCellValue(salary.getSalary());
            row.createCell(2).setCellValue(salary.getBonus());
            row.createCell(3).setCellValue(salary.getCutsalary());
            row.createCell(4).setCellValue(salary.getWar()==1? "月薪":"年薪");
            row.createCell(5).setCellValue(salary.getRevenue()==1? "税前":"税后");
            row.createCell(6).setCellValue(salary.getFund());
            row.createCell(7).setCellValue(salary.getPaydate());
            row.createCell(8).setCellValue(salary.getTotalsalary());
        }
        //写入数据
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @Override
    public void delete(long id) {
        mapper.deleteByPrimaryKey(id);
    }
}
